package com.alison.base;


import java.util.HashMap;
import java.util.IdentityHashMap;

/**
 * @Author alison
 * @Date 2024/4/5 20:37
 * @Version 1.0
 * @Description
 */
public class IdentityMapDemo {

    public static void main(String[] args) {
        IdentityHashMap<Object, Object> objectObjectIdentityHashMap = new IdentityHashMap<>();
        String str = new String("a");
        objectObjectIdentityHashMap.put(str, "1");
        objectObjectIdentityHashMap.put(str, "2");
        objectObjectIdentityHashMap.put(str, "3");
        String res = String.format("size: %d, key: %s, value: %s", objectObjectIdentityHashMap.size(), "new String('a')", objectObjectIdentityHashMap.get(str));
        System.out.println(res);
        System.out.println(objectObjectIdentityHashMap);
        System.out.println("----------------");

        objectObjectIdentityHashMap.put(new String("a"), "1");
        objectObjectIdentityHashMap.put(new String("a"), "2");
        objectObjectIdentityHashMap.put(new String("a"), "3");
        res = String.format("size: %d, key: %s, value: %s", objectObjectIdentityHashMap.size(), "new String('a')", objectObjectIdentityHashMap.get(new String("a")));
        System.out.println(res);
        System.out.println(objectObjectIdentityHashMap);

        System.out.println("----------------");
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put(str, "1");
        objectObjectHashMap.put(str, "2");
        objectObjectHashMap.put(str, "3");
        res = String.format("size: %d, key: %s, value: %s", objectObjectHashMap.size(), "new String('a')", objectObjectHashMap.get(str));
        System.out.println(res);
        System.out.println(objectObjectIdentityHashMap);
        System.out.println("----------------");

        objectObjectHashMap.put(new String("a"), "1");
        objectObjectHashMap.put(new String("a"), "2");
        objectObjectHashMap.put(new String("a"), "3");
        res = String.format("size: %d, key: %s, value: %s", objectObjectHashMap.size(), "new String('a')", objectObjectHashMap.get(new String("a")));
        System.out.println(res);
        System.out.println(objectObjectIdentityHashMap);
        System.out.println("hashcode: " + new String("a").hashCode());
        System.out.println("hashcode: " + new String("a").hashCode());
    }
}
